package com.example.lp.lpuicore.views.drawable;

/**
 * Author by lp,on 2021/6/17/017,13:58.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.lp.lpuicore.R;

public class MyCircleImageView extends View {

    private Bitmap headBitmap;  //头像图片
    private int radius;         //圆的半径
    private Paint paint;        //自定义画笔
    private Matrix matrix;
    private Shader shader;

    public MyCircleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        getCustomType(context, attrs);
        init();
    }

    private void init() {
        shader = new BitmapShader(headBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        matrix = new Matrix();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);
    }

    /**
     * 设置头像
     * */
    public void setHeadBitmap(Bitmap headBitmap) {
        shader = new BitmapShader(headBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeX = MeasureSpec.getMode(widthMeasureSpec);
        int modeY = MeasureSpec.getMode(heightMeasureSpec);
        int sizeX = MeasureSpec.getSize(widthMeasureSpec);
        int sizeY = MeasureSpec.getSize(heightMeasureSpec);
        //width和height都是EXACTLY(精确)的测量模式
        if (modeX == MeasureSpec.EXACTLY && modeY == MeasureSpec.EXACTLY) {
            radius = sizeX < sizeY ? sizeX / 2 : sizeY / 2;
        } else if (modeX == MeasureSpec.EXACTLY) {
            radius = sizeX / 2;
        } else if (modeY == MeasureSpec.EXACTLY) {
            radius = sizeY / 2;
        }
        //width和height都是AT_MOST(尽可以大)的测量模式，或者是UNSPECIAL(无上限)
        else {
            radius = headBitmap.getWidth() < headBitmap.getHeight() ? headBitmap.getWidth() / 2 : headBitmap.getHeight() / 2;
        }
        initPaintShader();
        //保存测量宽度和测量高度
        setMeasuredDimension(radius * 2, radius * 2);
    }

    private void initPaintShader() { //初始化画笔shader
        float scaleX = 1, scaleY = 1;
        //如果图片与圆的直径不一致，等比例缩放图片
        if (headBitmap.getWidth() != radius * 2 || headBitmap.getHeight() != radius * 2) {
            scaleX = (radius * 2) / (headBitmap.getWidth() * 1.0f);
            scaleY = (radius * 2) / (headBitmap.getHeight() * 1.0f);
        }
        matrix.setScale(scaleX, scaleY);
        shader.setLocalMatrix(matrix);
        paint.setShader(shader);
    }

    //获取自定义属性
    private void getCustomType(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
        headBitmap = BitmapFactory.decodeResource(getResources(), array.getResourceId(R.styleable.CircleImageView_circle_src, 0));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int realRadius = radius;
        //根据padding值，获取真实的圆的半径
        if (getPaddingLeft() + getPaddingRight() >= getPaddingTop() + getPaddingBottom())
            realRadius = (realRadius * 2 - getPaddingLeft() - getPaddingRight()) / 2;
        else
            realRadius = (realRadius * 2 - getPaddingTop() - getPaddingBottom()) / 2;
        //根据padding值，设置圆心的真实坐标
        canvas.drawCircle(radius + getPaddingLeft() - getPaddingRight(), radius + getPaddingTop() - getPaddingBottom(), realRadius, paint);
    }
}